<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About_us extends MY_Controller {
	function __construct() {
		parent::__construct();

		$this->load->model('type_model', '', TRUE);
		$this->load->model('con_common_model', '', TRUE);
		$this->load->model('con_photo_model', '', TRUE);
		$this->load->model('con_team_model', '', TRUE);
		$this->load->model('position_model', '', TRUE);
		$this->load->model('region_model', '', TRUE);
	}
	
	public function common() {
		if ($this->input->is_ajax_request()) {
			$type = $this->input->post("type");
			$selector = $this->input->post("selector");
			$operate = $this->input->post("operate");
			$is_submit = ($this->input->post("is_submit") == 'false') ? FALSE : TRUE;
			$form_data['textarea'] = $this->input->post("form_textarea_data");
			$form_data['fields_data'] = $this->input->post("form_fields_data");
			
			if (!$is_submit) {
				eval("\$data = \$this->" . $type . "(\$type, \$selector, \$operate);");
				
				$data['selector'] = $selector;
				$data['operate'] = $operate;
				
				$data['selector_data'] = $this->_get_current_editable_obj($type, $selector);
				
				$this->load->view('backend/' . $this->parent_type . '/' . $type, $data);
			} else {
				eval("\$data = \$this->" . $type . "(\$type, \$selector, \$operate, true, \$form_data);");
				
				if (!empty($data['res'])) {
					$this->output->set_output(json_encode($data['res']));
				} else {
					$this->output->set_output(json_encode('success'));
				}
			}
		}
	}
	
	public function company($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('#front'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
				} else {
					$upload_data = $this->_upload_pic();
					
					/*echo "<pre>";
					print_r($upload_data);*/
					
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					
					$pic = $this->con_photo_model->search_by_condition($condition, TRUE);
					
					$update_data['id'] = $pic['id'];
					$update_data['photo_path'] = $upload_data['file_db_path'];
					$update_data['upload_time'] = time();
					$this->con_photo_model->update($update_data);
				}
				break;
			case selector_replace('.company #intro'):
				if (!$is_submit) {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					$data['intro'] = $this->con_common_model->search_by_condition($condition, TRUE);
				} else {
					$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
					$intro = $this->con_common_model->search_by_condition($condition, TRUE);
					
					$update['id'] = $intro['id'];
					$update['content'] = $form_data['textarea'];
					$this->con_common_model->update($update);
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function design_team($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('.gallery'):
				if ($operate == 'create') {
					if (!$is_submit) {
						
					} else {
						$editable_obj = $this->_get_current_editable_obj($type, $selector);
						$upload_data = $this->_upload_pic('', TRUE, array($editable_obj['detail']['thumb']['width'], $editable_obj['detail']['thumb']['height']));
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save member info
						$temp = $this->_get_info_by_filename($upload_data['orig_name'], array('name', 'province', 'city', 'position'));
						
						$save_data['name'] = $temp['name'];
						
						$region_name['province'] = $temp['province'];
						$region_name['city'] = $temp['city'];
						$region_info = $this->region_model->_get_region_by_name($region_name);
						$save_data['region'] = $region_info['id'];
						
						$condition['name'] = $temp['position'];
						$position = $this->position_model->search_by_condition($condition, TRUE);
						$save_data['position'] = $position['id'];
						
						$max_weight = $this->_get_max_weight($this->con_team_model->search_by_condition());
						$save_data['weight'] = $max_weight + 1;
						$save_data['is_visible'] = TRUE;
						
						$member_id = $this->con_team_model->save($save_data);
						
						// save member photo
						$save_data2['con_id'] = $member_id;
						$save_data2['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data2['photo_path'] = $upload_data['file_db_path'];
						$save_data2['thumb_path'] = $upload_data['thumb_db_path'];
						$save_data2['upload_time'] = time();
						$save_data2['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data2);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$data['members'] = $this->con_team_model->search_by_condition();
						
						foreach ($data['members'] as $k => $v) {
							$condition['con_id'] = $v['id'];
							$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
							$data['members'][$k]['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
						}
					} else {
						$this->_update_weight($this->input->post("member_weight"), 'team');
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_team_model->update($update_data);
					}
				}
				break;
			case selector_replace('#history'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$member_id = $this->input->post("member_id");
						$condition['con_id'] = $member_id;
						$condition['type'] = $this->type_model->_get_by('variable', 'history', 'id', TRUE);
						
						$data['history'] = $this->con_common_model->search_by_condition($condition);
						/*echo "<pre>";
						print_r($data);*/
					} else {
						$this->_update_weight($this->input->post("history_weight"), 'common');
					}
				} elseif ($operate == 'edit_text') {
					if (!$is_submit) {
						
					} else {
						$mod_id = $this->input->post("mod_id");
						$mod_text = $this->input->post("mod_text");
						$member_id = $this->input->post("member_id");
						
						if (!empty($mod_id)) {
							$update['id'] = $mod_id;
							$update['name'] = $mod_text;
							$this->con_common_model->update($update);
						} else {
							$save['con_id'] = $member_id;
							$save['type'] = $this->type_model->_get_by('variable', 'history', 'id', TRUE);
							$save['name'] = $mod_text;
							$save['create_time'] = time();
							
							$condition['con_id'] = $member_id;
							$condition['type'] = $save['type'];
							$max_weight = $this->_get_max_weight($this->con_common_model->search_by_condition($condition));
							$save['weight'] = $max_weight + 1;
							
							$save['is_visible'] = NOT_DELETE;
							$data['res']['id'] = $this->con_common_model->save($save);
						}
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_common_model->update($update_data);
					}
				}
				break;
			case selector_replace('#portrait'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$member_id = $this->input->post("member_id");
						$_SESSION['current_edit']['member_id'] = $member_id;
					} else {
						$member_id = $_SESSION['current_edit']['member_id'];
						
						$editable_obj = $this->_get_current_editable_obj($type, $selector);
						$upload_data = $this->_upload_pic('', TRUE, array($editable_obj['detail']['thumb']['width'], $editable_obj['detail']['thumb']['height']));
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save member info
						$update_data['id'] = $member_id;
						
						$temp = $this->_get_info_by_filename($upload_data['orig_name'], array('name', 'province', 'city', 'position'));
						
						$update_data['name'] = $temp['name'];
						
						$region_name['province'] = $temp['province'];
						$region_name['city'] = $temp['city'];
						$region_info = $this->region_model->_get_region_by_name($region_name);
						$update_data['region'] = $region_info['id'];
						
						$condition['name'] = $temp['position'];
						$position = $this->position_model->search_by_condition($condition, TRUE);
						$update_data['position'] = $position['id'];
						
						$this->con_team_model->update($update_data);
						
						// save member photo
						unset($condition);
						$condition['con_id'] = $member_id;
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$photo = $this->con_photo_model->search_by_condition($condition, TRUE);
						
						$update_data2['id'] = $photo['id'];
						$update_data2['photo_path'] = $upload_data['file_db_path'];
						$update_data2['thumb_path'] = $upload_data['thumb_db_path'];
						$update_data2['upload_time'] = time();
						$this->con_photo_model->update($update_data2);
						
						//unset($_SESSION['current_edit']['member_id']);
					}
				}
				break;
			case selector_replace('#scrollbar1 .viewport'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$member_id = $this->input->post("member_id");
						$condition['con_id'] = $member_id;
						$condition['type'] = $this->type_model->_get_by('variable', 'design', 'id', TRUE);
						
						$data['design'] = $this->con_common_model->search_by_condition($condition);
						/*echo "<pre>";
						print_r($data);*/
					} else {
						$this->_update_weight($this->input->post("design_weight"), 'common');
					}
				} elseif ($operate == 'edit_text') {
					if (!$is_submit) {
						
					} else {
						$mod_id = $this->input->post("mod_id");
						$mod_text = $this->input->post("mod_text");
						$member_id = $this->input->post("member_id");
						
						if (!empty($mod_id)) {
							$update['id'] = $mod_id;
							$update['name'] = $mod_text;
							$this->con_common_model->update($update);
						} else {
							$save['con_id'] = $member_id;
							$save['type'] = $this->type_model->_get_by('variable', 'design', 'id', TRUE);
							$save['name'] = $mod_text;
							$save['create_time'] = time();
							
							$condition['con_id'] = $member_id;
							$condition['type'] = $save['type'];
							$max_weight = $this->_get_max_weight($this->con_common_model->search_by_condition($condition));
							$save['weight'] = $max_weight + 1;
							
							$save['is_visible'] = NOT_DELETE;
							$data['res']['id'] = $this->con_common_model->save($save);
						}
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_common_model->update($update_data);
					}
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function events($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('#gallery'):
				if ($operate == 'create') {
					if (!$is_submit) {
						/*$year = $this->input->post("year");
						$condition['year'] = $year;
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						
						$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);*/
						/*echo "<pre>";
						print_r($data['pic']);*/
						$year = $this->input->post("year");
						$_SESSION['current_edit']['year'] = $year;
					} else {
						$year = $_SESSION['current_edit']['year'];
						
						$editable_obj = $this->_get_current_editable_obj($type, $selector);
						$upload_data = $this->_upload_pic();
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save photo
						$save_data['year'] = $year;
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data['photo_path'] = $upload_data['file_db_path'];
						$save_data['upload_time'] = time();
						
						$condition['year'] = $year;
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_photo_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$year = $this->input->post("year");
						$condition['year'] = $year;
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
					} else {
						$this->_update_weight($this->input->post("pic_weight"), 'photo');
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_photo_model->update($update_data);
					}
				}
				break;
			case selector_replace('#scrollbar2 .viewport'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$year = $this->input->post("year");
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$condition['year'] = $year;
						$data['events'] = $this->con_common_model->search_by_condition($condition);
						/*echo "<pre>";
						print_r($data);*/
					} else {
						$this->_update_weight($this->input->post("events_weight"), 'common');
					}
				} elseif ($operate == 'edit_text') {
					if (!$is_submit) {
						
					} else {
						$mod_id = $this->input->post("mod_id");
						$mod_text = $this->input->post("mod_text");
						$year = $this->input->post("year");
						
						if (!empty($mod_id)) {
							$update['id'] = $mod_id;
							$update['name'] = $mod_text;
							$this->con_common_model->update($update);
						} else {
							$save['year'] = $year;
							$save['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
							$save['name'] = $mod_text;
							$save['create_time'] = time();
							$save['is_visible'] = NOT_DELETE;
							$data['res']['id'] = $this->con_common_model->save($save);
						}
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_common_model->update($update_data);
					}
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function honor($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		switch ($selector) {
			case selector_replace('#honor_carousel .viewport'):
				if ($operate == 'create') {
					if (!$is_submit) {
						
					} else {
						$editable_obj = $this->_get_current_editable_obj($type, $selector);
						$upload_data = $this->_upload_pic();
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save photo
						$save_data['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$save_data['photo_path'] = $upload_data['file_db_path'];
						$save_data['upload_time'] = time();
						
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_photo_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						
						$save_data['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
					} else {
						$this->_update_weight($this->input->post("pic_weight"), 'photo');
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_photo_model->update($update_data);
					}
				}
				break;
			case selector_replace('#scrollbar3 .viewport'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						
						$data['honor'] = $this->con_common_model->search_by_condition($condition);
						/*echo "<pre>";
						print_r($data);*/
					} else {
						$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
						$intro = $this->con_common_model->search_by_condition($condition, TRUE);
						
						$update['id'] = $intro['id'];
						$update['content'] = $form_data['textarea'];
						$this->con_common_model->update($update);
					}
				} elseif ($operate == 'edit_text') {
					if (!$is_submit) {
						
					} else {
						$mod_id = $this->input->post("mod_id");
						$mod_text = $this->input->post("mod_text");
						
						if (!empty($mod_id)) {
							$update['id'] = $mod_id;
							$update['name'] = $mod_text;
							$this->con_common_model->update($update);
						} else {
							$save['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
							$save['name'] = $mod_text;
							$save['create_time'] = time();
							$save['is_visible'] = NOT_DELETE;
							$data['res']['id'] = $this->con_common_model->save($save);
						}
					}
				} elseif ($operate == 'delete') {
					if (!$is_submit) {
						
					} else {
						$id = $this->input->post("id");
						$is_delete = ($this->input->post("is_delete") == 'true') ? IS_DELETE : NOT_DELETE;
						
						$update_data['id'] = $id;
						$update_data['is_visible'] = $is_delete;
						$this->con_common_model->update($update_data);
					}
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
	public function manager_community($type, $selector, $operate, $is_submit = FALSE, $form_data = NULL) {
		$condition['is_manager'] = TRUE;
		$manager = $this->con_team_model->search_by_condition($condition, TRUE);
		unset($condition['is_manager']);
		
		$sub_page_type = $this->input->post("sub_page_type");
		$data['sub_page_type'] = $sub_page_type;
		
		switch ($selector) {
			case selector_replace('.flip_sub_page'):
				if ($operate == 'create') {
					if (!$is_submit) {
						$_SESSION['current_edit']['sub_page_type'] = $sub_page_type;
					} else {
						$sub_page_type = $_SESSION['current_edit']['sub_page_type'];
						
						$upload_data = $this->_upload_pic();
						
						/*echo "<pre>";
						print_r($upload_data);
						return ;*/
						
						// save design or teach info
						if ($sub_page_type == 'design') {
							$name_pattern = array('name', 'year');
						} elseif ($sub_page_type == 'teach' || $sub_page_type == 'awards') {
							$name_pattern = array('name', 'sub_title');
						} elseif ($sub_page_type == 'treatise') {
							$name_pattern = array('name');
						}
						
						$save_data = $this->_get_info_by_filename($upload_data['orig_name'], $name_pattern);
						
						$save_data['type'] = $this->type_model->_get_by('variable', $sub_page_type, 'id', TRUE);
						
						$condition['con_id'] = $manager['id'];
						$condition['type'] = $save_data['type'];
						$max_weight = $this->_get_max_weight($this->con_common_model->search_by_condition($condition));
						$save_data['weight'] = $max_weight + 1;
						$save_data['is_visible'] = TRUE;
						
						$save_id = $this->con_common_model->save($save_data);
						
						// save design or teach photo
						$save_data2['con_id'] = $save_id;
						$save_data2['type'] = $save_data['type'];
						$save_data2['thumb_path'] = $upload_data['file_db_path'];
						$save_data2['upload_time'] = time();
						$save_data2['weight'] = 1;
						$save_data2['is_visible'] = TRUE;
						$this->con_photo_model->save($save_data2);
					}
				} elseif ($operate == 'edit') {
					if (!$is_submit) {
						$condition['con_id'] = $manager['id'];
						$condition['type'] = $this->type_model->_get_by('variable', $sub_page_type, 'id', TRUE);
						$data['content'] = $this->con_common_model->search_by_condition($condition);
						
						foreach ($data['content'] as $k => $v) {
							$condition['con_id'] = $v['id'];
							$data['content'][$k]['pic'] = $this->con_photo_model->search_by_condition($condition);
						}
					} else {
						$this->_update_weight($this->input->post("content_weight"), 'common');
					}
				} elseif ($operate == 'delete') {
					$this->_delete_item('common');
				}
				break;
			case selector_replace('.flip-block'):
				if ($operate == 'edit') {
					if (!$is_submit) {
						$content_id = $this->input->post("content_id");
						$data['content'] = $this->con_common_model->_get_by_id($content_id);
					} else {
						$update = $this->_get_form_array($form_data['fields_data']);
						$update['content'] = $form_data['textarea'];
						
						$this->con_common_model->update($update);
					}
				}
				break;
		}
		
		if (empty($data)) { $data = NULL; }
		
		return $data;
	}
	
}